﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.List
{

    /// <summary>
    /// a = [1,3,5,7,9,11];
    /// drop(a, 2);
    /// echo(a);
    /// # result is [1,5,7,9,11];
    /// </summary>
    public class DropNode : FuncNode
    {
        public DropNode()
            : base()
        {
            this.Value = "drop";
        }

        public override object Evaluate()
        {
            int length = Parameters.Count;
            if (Parameters.Count < 2)
            {
                throw new ArgumentException(
                    "Usage: drop(list, index)");
            }

            object list = Parameters[length - 1].Evaluate();
            if (!(list is List<object>))
            {
                throw new ArgumentException(
                    "The first parameter must be a list");
            }

            object idx = Parameters[length - 2].Evaluate();
            if (!Utils.IsNumeric(idx))
            {
                throw new ArgumentException(
                    "The second parameter must be a number.");
            }

            List<object> lst = list as List<object>;
            int nth = Convert.ToInt32(idx);

            return lst.Remove(nth);
        }
    }
}
